package com.shxy.rlzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shxy.rlzy.entity.Role;
import com.shxy.rlzy.entity.User;
import com.shxy.rlzy.entity.UserRole;
import com.shxy.rlzy.mapper.UserRoleMapper;
import com.shxy.rlzy.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanhao
 * @since 2021-08-16
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserRole getUserRoleByName(String userName) {
        return userRoleMapper.getByUserName(userName);
    }

    @Override
    public UserRole getUserRoleByUserId(Long userId) {
        return this.getOne(new QueryWrapper<UserRole>().eq("user_id", userId));
    }

    @Override
    public boolean set(User user, Role role) {
        UserRole userRole = userRoleMapper.getByUserId(user.getId());
        if(userRole == null) {
            userRole = new UserRole();
            userRole.setUserId(user.getId());
        }
        userRole.setRoleId(role.getId());
        return this.saveOrUpdate(userRole);
    }

}
